package cc.sunwl.study.java8.流API;

import cc.sunwl.study.java8.流API.domain.Dish;

import java.util.*;

import static java.util.stream.Collectors.*;

public class StreamTest {
    public static void main(String[] args) {
        // toList();
        // 可以得到，总计，总和，最小值，最大值，平均值summarizingInt()
        List<Dish> list = new LinkedList<>();
        // 获取每组价格最贵的
        Map<String, Dish> result = list.stream().collect(groupingBy(Dish::getType, collectingAndThen(maxBy(Comparator.comparing(Dish::getPrice)), Optional::get)));
        System.out.println("result = " + result);
        // 计算每组总价格
        list.stream().collect(groupingBy(Dish::getType, summingInt(Dish::getPrice)));
        // 分区，以boolean类型来分组，以500分区
        // eclipse ctrl+2+L idea ctrl+alt+V
        Map<Boolean, List<Dish>> result1 = list.stream().collect(partitioningBy(e -> e.getPrice() > 500));
        System.out.println("result1 = " + result1);
        Map<Boolean, Map<String, List<Dish>>> result2 = list.stream().collect(partitioningBy(e -> e.getPrice() > 500, groupingBy(Dish::getType)));
        System.out.println("result2 = " + result2);
    }
}
